# -*- coding: utf-8 -*-
"""Public section, including homepage and signup."""
from flask import Blueprint, flash, redirect, render_template, request, url_for, jsonify
from flask_login_multi import login_required, login_user, logout_user

from main.extensions import login_manager
from main.public.forms import LoginForm
from main.user.forms import RegisterForm
from main.user.models import User,Admin
from main.utils import flash_errors,SWJsonify
from main.helpers import templated
from .routes import reg_url

# from main.models import SmUser
from main.public.models import Product

from . import bp
reg_url(bp)


@login_manager.user_loader
def load_user(user_id,endpoint='user'):
    """Load user by ID."""
    if request.blueprint == 'admin':
        return Admin.query.get(int(user_id))
    else:
        # return User.get_by_id(int(user_id))
        return User.query.get(int(user_id))


@login_required
@templated()
def home():
    """Home page."""
    return dict()


@login_required
def logout():
    """Logout."""
    logout_user()
    flash('You are logged out.', 'info')
    return redirect(url_for('public.home'))


@templated()
def register():
    """Register new user."""
    username = request.args.get('username')
    return dict(username=username)


@templated()
def about():
    """About page."""
    form = LoginForm(request.form)
    return dict(form=form)

def getjson():
    product = Product.query.all()
    return SWJsonify({'result':product})


@templated()
def login():
    """login."""
    try:
        form = LoginForm(request.form)

        print()
        if request.method == 'POST':
            if form.validate_on_submit():
                login_user(form.user)
                flash('登录成功.', 'success')
                redirect_url = request.args.get('next') or url_for('user.index')
                return redirect(redirect_url)
            else:
                # form.verification_code.data = ''
                # flash_errors(form)
                return 'error'


    except Exception as e:
        print(str(e))
    

    return dict(form=form)



